//游戏主类
#include "stdafx.h"
#include "mainwindow.h"
#include "game.h"
#include "./ui_mainwindow.h"
//#include "geom.h"

CGame::CGame () {
    for (int x = 0; x < MM * SI + 100; x++) {
        for (int y = 0; y < MN; y++)
            fir_plt[x][y] = plt_num[x][y] = -1;
    }
    for (int i = 0; i < MN; i++)
        row_zmb[i] = lst[i] = 0;
    started = zk = false;
    XS = 0;
    t_ai = m_cc = 0;
}
CGame::~CGame () {}
void CGame::DestroyJs (int z) //删除僵尸
{
    if(zombies[z].type<100)
    {
        m_wnd->dis_sum+=zombies[z].x*zombies[z].x;
        if(zombies[z].x<MM*SI/2)m_wnd->dang_cnt+=1;
    }
    if (zombies[z].fei) //化肥
    {
        m_wnd->z_fs += 1;
        if (m_wnd->beans < 5)
            m_wnd->beans += 1;
    }
    zombies[z].life = -1;
    row_zmb[zombies[z].y] -= 1;
    if (SZZ)
        return; //正在双指针
    zmb_cnt -= 1;
    if (z < zmb_cnt) {
        zombies[z] = zombies[zmb_cnt];
        zombies[z].num = z;
    }
}
void CGame::CalZw () //计算植物信息
{
    for (int y = 0; y < MN; y++) {
        for (int x = 0; x < MM * SI + 100; x++)
            fir_plt[x][y] = plt_num[x][y] = -1;
    }
    for (int i = 0; i < plt_cnt; i++)
        fir_plt[plants[i].x][plants[i].y] = plt_num[plants[i].x][plants[i].y] = i;
    for (int y = 0; y < MN; y++) {
        for (int x = 1; x < MM * SI + 100; x++) {
            if (fir_plt[x][y] == -1)
                fir_plt[x][y] = fir_plt[x - 1][y]; //第一个植物
        }
    }
}
int CGame::DestroyZw (int z) //删除植物
{
    if(!over)
    {
        m_wnd->eat_sum+=1;
        if(!m_wnd->prot[plants[z].type])
            m_wnd->eat_cnt+=1;
    }
    if(plants[z].type==-1)
    {
        int t=plants[z].attack;
        if(t>0)
        {
            if(m_wnd->SHU<MXUAN)
            {
                m_wnd->m_xuan[m_wnd->SHU++]=t-1;
                lagz[plants[z].x/SI][plants[z].y]=TM;
            }
            else if(!over)return 0;
        }
        else
        {
            Zombie js=m_wnd->zombies[(-t)-1];
            js.x=plants[z].x;
            js.y=plants[z].y;
            NewJs(js);
        }
    }
    m_wnd->z_fs += plants[z].fei; //返还
    int tt = -1, rt = 0;
    for (int i = 0; i < m_wnd->tmr_cnt; i++) //寻找对应计时器
    {
        if (m_wnd->tmr[i].id == z) {
            tt = i;
            break;
        }
    }
    int v_tm=m_wnd->v_tm;
    if(!m_wnd->b_fast)v_tm=m_wnd->tim.elapsed ();
    rt = m_wnd->tmr[tt].nxt - v_tm; //返回这个植物距离发射还有多久(2048)
    if (rt < 0)
        rt = 0;
    plants[z].life = -1;
    m_wnd->KillTimer (z);
    m_wnd->KillTimer (z + 10000);//删除计时器和大招计时
    plt_cnt -= 1;
    if (z < plt_cnt) {
        plants[z] = plants[plt_cnt];
        plants[z].num = z;
        int tt = -1;
        for (int i = 0; i < m_wnd->tmr_cnt; i++) {
            if (m_wnd->tmr[i].id == plt_cnt) {
                tt = i;
                break;
            }
        }
        if (tt == -1)
            QMessageBox::critical (m_wnd, tr (""), tr ("BUG"));
        int te = m_wnd->tmr[tt].nxt - v_tm;
        if (te < 0)
            te = 0;
        m_wnd->KillTimer (plt_cnt); //重置计时器
        m_wnd->NewTimer (z, plants[z].time, te); //更换计时器
        if (plants[z].time == 100) //重置大招
        {
            tt = -1;
            for (int i = 0; i < m_wnd->tmr_cnt; i++) {
                if (m_wnd->tmr[i].id == plt_cnt + 10000) {
                    tt = i;
                    break;
                }
            }
            if (tt == -1)
                QMessageBox::critical (m_wnd, tr (""), tr ("BUG"));
            te = m_wnd->tmr[tt].nxt - v_tm;
            if (te < 0)
                te = 0;
            m_wnd->KillTimer (plt_cnt + 10000);
            m_wnd->NewTimer (z + 10000, plants[z].dztm, te); //更换计时器
        }
    }
    CalZw ();
    return rt;
}
void CGame::DestroyZw (int x, int y) //按照位置删除
{
    int shu = GetZwShu (x, y);
    if (shu == -1)
        return;
    DestroyZw (shu);
}
void CGame::DestroyZd (int z) //删除子弹
{
    if (bullets[z].spd < 0 && bullets[z].siz == -2) //小车子弹
        car_driv[bullets[z].y] = false; //清除正在小车的状态
    bullets[z].y = -1;
    bul_cnt -= 1;
    if (z < bul_cnt)
        bullets[z] = bullets[bul_cnt];
}
int CGame::GetZwShu (int x, int y) const {
    if (x < 0 || x >= MM * SI + 100 || y < 0 || y >= MN)
        return -1;
    return plt_num[x][y];
}
bool CGame::meihuo (int jshu) {
    if (zombies[jshu].type == 11)
        return false; //不能魅惑BOSS
    if ((zombies[jshu].type == 9 || zombies[jshu].type == 12) && rand () % 10 < 9) //小车和巨人概率为十分之一
        return false;
    Bullet zd;
    zd.x = zombies[jshu].x;
    zd.y = zombies[jshu].y;
    zd.spd = -1;
    zd.siz = zombies[jshu].type;
    zd.attack = zombies[jshu].life;
    zd.zx = zombies[jshu].id;
    zd.zy = 0;
    NewZd (zd);
    DestroyJs (jshu); //把僵尸变为子弹
    return true;
}
bool CGame::Eat (int zshu, int jshu,bool snd=0) //吃,返回是否被杀死
{
    if(plants[zshu].type==-1&&zombies[jshu].type!=9)
    {
        if(!snd||zombies[jshu].type!=20)
            zombies[jshu].x-=zombies[jshu].spd;
        zombies[jshu].eating=false;
        return false;
    }
    if(zombies[jshu].type==20)
        zombies[jshu].eat=(plants[zshu].life>150?4:2);
    if (zombies[jshu].type == 15&&plants[zshu].type!=7) //爆炸
    {
        Crepeer (jshu);
        return true;
    }
    if (plants[zshu].id == 126 && plants[zshu].life > zombies[jshu].eat) //越过全息坚果
    {
        zombies[jshu].x -= zombies[jshu].spd * 2;
        return false;
    }
    if (zombies[jshu].jump) //特殊吃法
    {
        int t = GetJsEat (plants[zshu], zombies[jshu], *this); //特殊吃法
        if (t)
            zombies[jshu].id = t;
        return t == 0;
    }
    int x = plants[zshu].x / SI, y = plants[zshu].y, et = zombies[jshu].eat;
    m_ng[x][y] -= et;
    et = 0; //先吃南瓜
    if (m_ng[x][y] < 0) {
        et = -m_ng[x][y];
        m_ng[x][y] = 0;
    }
    if (zombies[jshu].type == 9) //巨人抬脚
    {
        int z = m_wnd->m_ea;
        if (z > 2)
            z = 2;
        zombies[jshu].fss += 1 - rand () % z;
        if (m_wnd->m_hd)
            zombies[jshu].fss += 1;
        if (m_wnd->m_tui)
            zombies[jshu].fss += 1;
        if (zombies[jshu].fss >= 20) {
            if (plants[zshu].type == 1 && m_wnd->imjs)
                m_yg += plants[zshu].life * 5; //我是僵尸,向日葵
            plants[zshu].life -= et;
            zombies[jshu].fss = 0;
        }
    }
    else
        plants[zshu].life -= et;
    if (plants[zshu].type == 1 && m_wnd->imjs && zombies[jshu].type != 9) {
        m_yg += 5 * zombies[jshu].eat;
        if (plants[zshu].life < 0)
            m_yg += 5 * plants[zshu].life;
    }
    if (zombies[jshu].type < 100 && zombies[jshu].eat) //吃东西影响生命
    {
        if (plants[zshu].type == 35 || plants[zshu].type == 36 || plants[zshu].type==39) //反伤
        {
            if (zombies[jshu].type == 6) //大嘴
                zombies[jshu].life -= rand () % 5 == 0;
            else
                zombies[jshu].life -= rand () % 10 == 0;
        }
        else {
            if (zombies[jshu].type == 0)
                zombies[jshu].life += rand () % 200 == 0;
            else if (zombies[jshu].type == 1)
                zombies[jshu].life += rand () % 160 == 0;
            else if (zombies[jshu].type == 6) //大嘴
                zombies[jshu].life += rand () % 50 == 0;
            else if(zombies[jshu].type == 12&&!m_wnd->m_tui)
                zombies[jshu].life -= (m_wnd->m_hd?rand()%2:1);
            else if (zombies[jshu].type != 4)
                zombies[jshu].life += rand () % 100 == 0;
        }
        zombies[jshu].id = GetJsIcon (zombies[jshu], *this); //更新图标
        if (zombies[jshu].life <= 0) //僵尸死掉
        {
            if (plants[zshu].life <= 0) //吃光
            {
                plants[zshu].life = 0;
                DestroyZw (zshu);
            }
            DestroyJs (jshu);
            return true;
        }
    }
    bool rt = false;
    if (plants[zshu].type == 32 && (!plants[zshu].mush || scene == 2 || scene == 3) && et && zombies[jshu].type != 9 && zombies[jshu].type != 11 && (zombies[jshu].type != 12||zombies[jshu].life<=30)) //吃魅惑菇
    {
        rt = true;
        meihuo (jshu);
    }
    plants[zshu].id = GetZwIcon (plants[zshu]);
    if (plants[zshu].life <= 0) //吃光
    {
        plants[zshu].life = 0;
        DestroyZw (zshu);
    }
    return rt;
}
int CGame::GetFirstJs (int x, int y) {
    int zx = 10000, rtn = -1;
    for (int i = 0; i < zmb_cnt; i++) //遍历僵尸
    {
        if (zombies[i].life == -1)
            continue;
        if (zombies[i].x < zx && zombies[i].x > x && zombies[i].y == y && zombies[i].type < 100 &&zombies[i].type!=19) {
            rtn = i;
            zx = zombies[i].x;
        }
    }
    return rtn;
}
int CGame::GetLastJs (int x, int y) {
    int zd = -10000, rtn = -1;
    for (int i = 0; i < zmb_cnt; i++) //遍历僵尸
    {
        if (zombies[i].life == -1)
            continue;
        if (zombies[i].x > zd && zombies[i].x < x && zombies[i].y == y && zombies[i].type < 100 &&zombies[i].type!=19) {
            rtn = i;
            zd = zombies[i].x;
        }
    }
    return rtn;
}
int CGame::GetFirstZw (int x, int y) {
    if (x <= 0 || x - 1 >= MMM * MSI + 100)
        return -1;
    return fir_plt[x - 1][y];
}
bool CGame::Goj (int zshu) //植物攻击
{
    if (plants[zshu].mush && (scene == 0 || scene == 1 || scene == 4 || scene == 5))
        return false;
    plants[zshu].nxt_tm = TM + plants[zshu].time / 25;
    if (plants[zshu].attack < 0) //非标准植物
        return NotZw (zshu);
    if (plants[zshu].inst) { //土豆地雷和大嘴花
        plants[zshu].fu -= 1; //回复
        if (plants[zshu].fu < 0)
            plants[zshu].fu = 0;
        plants[zshu].id = GetZwIcon (plants[zshu]);
        int rtn = GetFirstJs (plants[zshu].x, plants[zshu].y);
        if (rtn == -1)
            return false;
        if (zombies[rtn].x < plants[zshu].x + (plants[zshu].freeze?100:250) * SI / 110 && plants[zshu].fu == 0) {
            KillJs (zombies[rtn].x, zombies[rtn].y, plants[zshu].attack);
            if (plants[zshu].freeze) { //土豆地雷
                DestroyZw (zshu);
                return true;
            }
            plants[zshu].fu = plants[zshu].move_spd;
            plants[zshu].id = GetZwIcon (plants[zshu]);
        }
        return false;
    }
    Bullet zd; //发射子弹
    zd.color = plants[zshu].color;
    zd.spd = plants[zshu].move_spd;
    zd.x = plants[zshu].x - 35;
    zd.y = plants[zshu].y;
    zd.attack = plants[zshu].attack;
    zd.siz = plants[zshu].siz;
    zd.freeze = plants[zshu].freeze;
    zd.tx = plants[zshu].x - 1;
    zd.zx = plants[zshu].x;
    zd.jian = plants[zshu].jian;
    zd.left=plants[zshu].left;
    if (zd.attack >= 3) { //西瓜冰瓜
        zd.sx = plants[zshu].x - 35;
        if (row_zmb[plants[zshu].y] > 0) {
            NewZd (zd);
            plants[zshu].tag = TM + plants[zshu].time / 25;
        }
    }
    else if (plants[zshu].type == 22) { //星星
        zd.tx=-1024;
        for (int y = 0; y < MN; y++) {
            if (row_zmb[y] > 0) {
                zd.y = y;
                zd.x = zd.sx = plants[zshu].x;
                zd.zx = plants[zshu].x;
                zd.zy = plants[zshu].y;
                zd.mh = (rand () % 100 == 0 && !m_wnd->imjs);// 1%魅惑
                NewZd (zd);
                zd.x = zd.sx = -120;//向左
                zd.mh = (rand () % 100 == 0 && !m_wnd->imjs);// 1%魅惑
                NewZd (zd);
            }
        }
    }
    else if (plants[zshu].type == 37 && row_zmb[plants[zshu].y] > 0) { //豌豆荚
        int t = plants[zshu].tag;
        for (int s = 0; s <= t; s++) {
            NewZd (zd);
            zd.x += SI / 3;
        }
    }
    else if(plants[zshu].type==41)
    {
        zd.tes=true;
        for (int z = -1; z <= 1; z++) {
            int y=plants[zshu].y+z;
            if (y>=0&&y<MN&&row_zmb[y] > 0) {
                zd.y = y;
                zd.x = zd.sx = plants[zshu].x;
                zd.zx = plants[zshu].x;
                zd.zy = plants[zshu].y;
                NewZd (zd);
            }
        }
    }
    else if (row_zmb[plants[zshu].y] > 0)
    {
        if(zd.color==QColor(0,0,0))//仙人掌
        {
            bool bk=false;
            for(int i=0;i<zmb_cnt;i++)
            {
                if(zombies[i].type==19&&zombies[i].y==plants[zshu].y)
                {
                    bk=true;
                    break;
                }
            }
            if(bk)//找到气球
                zd.siz=1;
        }
        NewZd (zd);
    }
    return false;
}
void CGame::NewZd (Bullet zd) {
    if (bul_cnt > 4900) //上限
        return;
    bullets[bul_cnt++] = zd;
}
void CGame::NewJs (Zombie js) {
    if (zmb_cnt > 800) //上限
        return;
    while (iswa[js.y] && js.type >= 0 && !m_wnd->can_wat[js.type])
        js.y = rand () % MN;
    row_zmb[js.y] += 1;
    if (row_zmb[js.y] == 1)//唤醒
        m_wnd->WakeUp (js.y);
    zombies[zmb_cnt] = js;
    zombies[zmb_cnt].num = zmb_cnt;
    zmb_cnt += 1;
}
bool CGame::ShiFei (Plant &zw) { //施肥
    if (zw.type == 7 || zw.type == 11 || zw.type == 17 || zw.type == 15 || zw.type == 18 || zw.type == 27 || zw.type == 30 || zw.type == 31 || zw.type == 32 || zw.type == 33 || zw.type == 34)
        return false;
    zw.fei += 1;
    zw.time /= 1.25;//加速
    if (zw.time == 100)//防止大招误判
        zw.time = 101;
    if (zw.fei == 2 && zw.jian == 0 && zw.type != 22)
        zw.jian += 1;//群体伤害
    if (zw.fei == 3 && zw.type != 22)
        zw.jian += 1;//群体伤害
    if (zw.type == 2 || zw.type == 6 || zw.type == 19)
        zw.life *= 1.7;//坚果
    zw.id = GetZwIcon (zw);
    m_wnd->KillTimer (zw.num);
    m_wnd->SetTimer (zw.num, zw.time, NULL);//更新计时器
    return true;
}
bool CGame::NewZw (Plant zw) {
    if(TM-m_ice[zw.x / SI][zw.y]< (m_wnd->m_hd ? 800 : 400) && zw.type!=5&&zw.type!=3)
        return false;
    if (m_keng[zw.x / SI][zw.y] == 1 || (m_keng[zw.x / SI][zw.y] == 2 && zw.type != 14) || (m_dc[zw.x / SI][zw.y]&& zw.type != 3)) //有坑，或有水且不是荷叶
        return false;
    m_yg -= zw.cost;
    if (m_yg < 0) {
        m_yg += zw.cost;
        return false;
    }
    if (zw.type == 38) { //化肥
        int z = GetZwShu (zw.x, zw.y);
        if (z != -1 && plants[z].fei < 5 && plants[z].time != 100) {
            if (m_wnd->ui->creative->isChecked ()) {
                while (plants[z].fei < 5) {
                    if (!ShiFei (plants[z]))
                        break;
                }
                return true;
            }
            else {
                if (ShiFei (plants[z]))
                    return true;
            }
        }
        m_yg += zw.cost;
        return false;
    }
    if (zw.type == 24) { //南瓜
        m_ng[zw.x / SI][zw.y] = 500;
        return true;
    }
    if (zw.type == 28) { //咖啡豆
        int z = GetZwShu (zw.x, zw.y);
        if (z != -1 && plants[z].mush) {
            plants[z].mush = false;
            return true;
        }
        m_yg += zw.cost;
        return false;
    }
    if (zw.type == 37) { //豌豆荚
        int t = GetZwShu (zw.x, zw.y);
        if (t != -1 && plants[t].type == 37) {
            if (plants[t].tag == 4) {
                m_yg += zw.cost;
                return false;
            }
            plants[t].tag += 1;
            plants[t].id = GetZwIcon (plants[t]);
            return true;
        }
    }
    if (zw.type == 14) { //荷叶
        if (m_keng[zw.x / SI][zw.y] == 2) {
            m_keng[zw.x / SI][zw.y] = 3;
            return true;
        }
        m_yg += zw.cost;
        return false;
    }
    if(zw.type==43)//翻转
    {
        int z = GetZwShu (zw.x, zw.y),x=plants[z].type;
        if (z != -1&&(x==0||x==4||x==8||x==9||x==10||x==41)){
            plants[z].left^=1;
            return true;
        }
        m_yg += zw.cost;
        return false;
    }
    if (GetZwShu (zw.x, zw.y) != -1 && zw.type != 3) //炸弹可以放在非空的地方
    {
        m_yg += zw.cost;
        return false;
    }
    if (zw.type == 29) { //地刺
        m_dc[zw.x / SI][zw.y] = 5;
        return true;
    }
    int i = plt_cnt,tt=zw.time;
    zw.time=(zw.time <= 5?200:zw.time);
    plants[i] = zw;
    plants[i].num = i;
    plants[i].id = GetZwIcon (plants[i]);
    plt_cnt += 1;
    double sd=m_wnd->ui->speed->value();
    if (zw.nxt_tm == -1)//初始状态
        m_wnd->NewTimer (i, zw.time, (zw.type==1||zw.type==20?zw.time/2:zw.time)/sd);
    else
        m_wnd->NewTimer (i, zw.time, zw.nxt_tm);
    zw.nxt_tm=TM+zw.time/25;
    if (tt <= 5)//瞬时
        Goj (i);
    CalZw ();//更新
    return true;
}
bool CGame::JsCai(int shu)
{
    int tx = (zombies[shu].x + 80 * SI / 110) / SI, ty = zombies[shu].y;
    if (tx >= 0 && tx < MM && zombies[shu].type < 100 && m_dc[tx][ty] && zombies[shu].type != 8 &&zombies[shu].type!=19) {//除了跳跳之外受到地刺伤害
        if (zombies[shu].type == 9 || zombies[shu].type == 11) //踩扁地刺
            m_dc[tx][ty] = 0;
        else if (zombies[shu].type == 12 && zombies[shu].life > 30) { //扎车
            zombies[shu].life = 30;
            m_dc[tx][ty] -= 1;
        }
        else if(rand () % 10 == 0)
            zombies[shu].life -= 1;
        zombies[shu].id = GetJsIcon (zombies[shu], *this);
        if (zombies[shu].life <= 0) {
            DestroyJs (shu);
            return true;
        }
    }
    if (tx >= 0 && tx < MM && zombies[shu].type == 12 && zombies[shu].life > 30 && m_wnd->r_gq != MAXGQS + 5)
        m_ice[tx][ty] = TM;//小车生成冰
    if (tx >= 0 && tx < MM && zombies[shu].type != 8 && m_keng[tx][ty] == 3 && zombies[shu].type < 100 && GetZwShu (tx * SI, ty) == -1 && !m_wnd->m_ai &&rand()%25==0)
        m_keng[tx][ty] = 2;//除了跳跳之外有概率踩掉荷叶
    return false;
}

void CGame::ZhangNan(int shu)
{
    for (int i = 0; i < zmb_cnt; i++) {
        if (i == shu || zombies[i].type >= 100 || zombies[i].life == -1 || zombies[i].type == 11 || zombies[i].type == 16)
            continue;
        if (zombies[i].x >= zombies[shu].x - SI && zombies[i].x <= zombies[shu].x + SI && zombies[i].y >= zombies[shu].y - 1 && zombies[i].y <= zombies[shu].y + 1&&zombies[shu].x>=SI)
            zombies[i].x -= SI;//瞬移
    }
    for (int i = 0; i < plt_cnt; i++) { //使用咒骂伤害植物
        if (plants[i].life != -1 && plants[i].y == zombies[shu].y&&plants[i].type!=-1) {
            plants[i].life /= 1.7;
            if (plants[i].life <= 0) {
                DestroyZw (i);
                i -= 1;
            }
            else
                plants[i].id = GetZwIcon (plants[i]);
        }
    }
}

bool CGame::MoveJs (int shu) { //移动僵尸,返回是否被杀死
    bool rt = false;
    if (zombies[shu].freeze % 2 == 0 || (zombies[shu].type == 12 && zombies[shu].life > 30) || zombies[shu].type == 11 || zombies[shu].type ==19 || zombies[shu].type >= 100) //若寒冰效果，速度变为一半，车不受影响
    {
        if(JsCai(shu))return true;
        int i = GetFirstZw (zombies[shu].x, zombies[shu].y);
        zombies[shu].x -= zombies[shu].spd;
        bool ee=false;
        if (i != -1 && zombies[shu].x - 80 * SI / 110 <= plants[i].x) {//吃
            zombies[shu].x += zombies[shu].spd;
            ee=zombies[shu].eating = true;//正在吃
            rt = Eat (i, shu);
            if (rt)
                return true;
        }
        if(zombies[shu].type==20)//喷火
        {
            int ti=GetFirstZw (zombies[shu].x-80*SI/110-5, zombies[shu].y);
            if (ti != -1 && zombies[shu].x - 200 * SI / 110 <= plants[ti].x) {//吃
                ee=zombies[shu].eating = true;//正在吃
                Eat (ti, shu,1);
            }
            int tx=zombies[shu].x/SI,ty = zombies[shu].y;
            if(m_dc[tx][ty])
            {
                ee=zombies[shu].eating = true;
                if(zombies[shu].x%20==0)
                    m_dc[tx][ty]-=1;
            }
            if(tx&&m_dc[tx-1][ty])
            {
                ee=zombies[shu].eating = true;
                if(zombies[shu].x%20==0)
                    m_dc[tx-1][ty]-=1;
            }
        }
        if(!ee) {
            if (zombies[shu].type == 9)//巨人下落
                zombies[shu].fss = 0;
            zombies[shu].eating = false;
        }
        if (zombies[shu].x < -110 && !over) {
            if (zombies[shu].type == 16) {//末影人
                DestroyJs (shu);
                return true;
            }
            if (zombies[shu].type == 11) //BOSS
            {
                zombies[shu].spd = -10;
                return false;
            }
            if (zombies[shu].type >= 100) //僵尸的子弹
            {
                DestroyJs (shu);
                return true;
            }
            if (car[zombies[shu].y]) //车
            {
                int ty = zombies[shu].y;
                car[ty] = false;
                car_driv[ty] = true;
                Bullet zd;//创建子弹
                zd.attack = 1e8;zd.siz = -2;
                zd.y = ty;zd.x = -150;
                zd.spd = -4;zd.zx = 28;
                NewZd (zd);
                DestroyJs (shu);
                bool bb = false;
                for (int i = 0; i < MN; i++) {
                    if (car[i]) {
                        bb = true;
                        break;
                    }
                }
                if (!bb && m_wnd->imjs) {//我是僵尸
                    KillAll ();
                    m_wnd->GameOver ();
                    QMessageBox::information (m_wnd, tr ("提示"), tr ("胜利"));
                }
                return true;
            }
            else if (!car_driv[zombies[shu].y]) {//正在使用车
                if (m_wnd->imjs) {
                    DestroyJs (shu);
                    return true;
                }
                over = true;
                int s = 0;
                for (int i = 0; i < zmb_cnt; i++) {
                    if (zombies[i].type < 100)
                        s += 1;//计算分数
                }
                KillAll ();
                m_wnd->GameOver ();
                int fs=m_wnd->jd - (m_wnd->is11 ? 0 : s);
                if(m_wnd->b_guan)fs=m_wnd->m_n;
                QMessageBox::information (m_wnd, tr ("提示"), QString::asprintf ("失败：\r\n得分:%d", fs));
            }
        }
        if (zombies[shu].x > MM * SI + 20 && zombies[shu].type != 11 && zombies[shu].spd < 0) {
            if (zombies[shu].type >= 100) {//子弹
                DestroyJs (shu);
                return true;
            }
            zombies[shu].spd = -zombies[shu].spd;
        }
    }
    if (zombies[shu].type == 15) //苦力怕走得慢
        zombies[shu].freeze ^= 1;
    else {
        zombies[shu].freeze += 1;
        if (zombies[shu].freeze > 100)
            zombies[shu].freeze = 100;
    }
    if (zombies[shu].shoot) {//发射
        zombies[shu].fss += 1;
        int zz = 35 * m_wnd->m_ea;//根据难度决定频率
        if (zz == 70)zz = 50;
        if (m_wnd->m_tui)zz = 24;
        else if (m_wnd->m_hd)zz = 30;
        if (zombies[shu].fss >= zz) //发射时间
        {
            zombies[shu].fss = 0;
            Fash (zombies[shu], *this);
        }
    }
    return rt;
}
void CGame::Crepeer (int i) {
    if (zombies[i].type == 15) //苦力怕
    {
        int x = (zombies[i].x / SI) * SI, y = zombies[i].y;
        for (int a = -2; a <= 1; a++) {
            for (int b = -1; b <= 1; b++) {//4*3爆炸
                int z = GetZwShu (x + a * SI, y + b);
                if (z != -1) {
                    int et = 0, tx = (x + a * SI) / SI, ty = y + b;
                    m_ng[tx][ty] -= 200;//南瓜抗伤
                    if (m_ng[tx][ty] < 0) {
                        et = -m_ng[tx][ty];
                        m_ng[tx][ty] = 0;
                    }
                    plants[z].life -= et;
                    if (plants[z].life <= 0)
                        DestroyZw (z);
                    else
                        plants[z].id = GetZwIcon (plants[z]);
                }
            }
        }
    }
    DestroyJs (i);
}
Bullet p_zid[5000];Zombie p_jsh[1000];
bool cmp1 (int a, int b) {
    return p_zid[a].x < p_zid[b].x;
}
bool cmp2 (int a, int b) {
    if(p_jsh[a].x == p_jsh[b].x)
        return p_jsh[a].freeze > p_jsh[b].freeze;
    return p_jsh[a].x < p_jsh[b].x;
}
void CGame::MoveAllZd () //双指针
{
    SZZ = true;bool snd=0;
    double sd = m_wnd->ui->speed->value ();
    if (sd > 1)
        sd = 1;
    int zs[MMN] = {0}, js[MMN] = {0};
    Bullet nw[200];//新的(魅惑)子弹
    int ns = 0;
    for (int i = 0; i < bul_cnt; i++) {
        int y = bullets[i].y, x = (bullets[i].x / SI) * SI, t = GetZwShu (x, y);
        if (t != -1 && plants[t].type == 36 && bullets[i].x - bullets[i].zx >= SI) { //火炬
            if (bullets[i].color == QColor (0, 205, 0))//普通豌豆
            {
                bullets[i].color = QColor (255, 0, 0), bullets[i].attack *= (plants[t].fei == 5 ? 2 : 1);
                if(bullets[i].jian==0)bullets[i].jian=1;
            }
            else if (bullets[i].color == QColor (0, 203, 0))//三线豌豆
            {
                bullets[i].color = QColor (254, 0, 0), bullets[i].attack *= (plants[t].fei == 5 ? 2 : 1);
                if(bullets[i].jian==0)bullets[i].jian=1;
            }
            else if (bullets[i].color == QColor (0, 255, 255))
                bullets[i].color = QColor (0, 204, 0), bullets[i].freeze = false;
        }
        pd[y][zs[y]++] = i;
        p_zid[i] = bullets[i];
    }
    for (int i = 0; i < zmb_cnt; i++) {
        if (zombies[i].type >= 100)
            continue;
        int y = zombies[i].y;
        pj[y][js[y]++] = i;
        p_jsh[i] = zombies[i];
    }
    for (int y = 0; y < MN; y++) { //枚举行
        std::sort (pd[y], pd[y] + zs[y], cmp1);
        std::sort (pj[y], pj[y] + js[y], cmp2);
        for (int a = 0, b = 0; a < zs[y]; a++) {
            int bul_i = pd[y][a], zx = bullets[bul_i].x;
            if (bullets[bul_i].spd > 0)
                bullets[bul_i].x += bullets[bul_i].spd * (bullets[bul_i].left?-2:2) * sd;
            else
                bullets[bul_i].x -= bullets[bul_i].spd * sd;
            while (b < js[y] && (zombies[pj[y][b]].x <= zx || zombies[pj[y][b]].life < 0))//找到右面的第一个
                b += 1;
            int zmb_i = (b == js[y] ? -1 : pj[y][b]);
            if(bullets[bul_i].left)
            {
                if(b>0)
                    bullets[bul_i].tx = zombies[pj[y][b-1]].x;
                else
                    bullets[bul_i].tx = -20;
            }
            else
            {
                if (zmb_i != -1)
                    bullets[bul_i].tx = zombies[zmb_i].x;
                else
                    bullets[bul_i].tx = MM * SI + 20;
            }
            if (bullets[bul_i].spd < 0) { //魅惑子弹
                if (bullets[bul_i].siz == 13) {//豌豆僵尸魅惑后打子弹
                    bullets[bul_i].zy += 1;
                    if (bullets[bul_i].zy >= 80) {
                        bullets[bul_i].zy = 0;
                        Bullet zd;zd.freeze = false;
                        zd.color = QColor (0, 255, 0);
                        zd.siz = 5;zd.attack = 1;zd.spd = 5;
                        zd.x = bullets[bul_i].x;zd.y = y;
                        nw[ns++] = zd;
                    }
                }
                else if (bullets[bul_i].siz == 14) {//冰瓜僵尸魅惑后打子弹
                    bullets[bul_i].zy += 1;
                    if (bullets[bul_i].zy >= 80) {
                        bullets[bul_i].zy = 0;
                        Bullet zd;zd.freeze = true;
                        zd.color = QColor (128, 128, 255);
                        zd.siz = 11;zd.attack = 4;zd.spd = 5;
                        zd.x = bullets[bul_i].x;zd.y = y;
                        nw[ns++] = zd;
                    }
                }
            }
            if(zmb_i!=-1&&zombies[zmb_i].type==19&& bullets[bul_i].color != QColor (10, 170, 255) && bullets[bul_i].color != QColor (0, 200, 0) && bullets[bul_i].color != QColor (0, 0, 0)&&bullets[bul_i].spd>0)//气球僵尸
            {
                bullets[bul_i].tx = MM * SI + 20;
                continue;
            }
            if(zmb_i!=-1&&zombies[zmb_i].type!=19&& bullets[bul_i].color == QColor (0, 0, 0) && bullets[bul_i].siz==1&&bullets[bul_i].spd>0)//尖刺气球优先
            {
                bullets[bul_i].tx = MM * SI + 20;
                continue;
            }
            if (zmb_i != -1 && bullets[bul_i].x + 50 >= zombies[zmb_i].x) { //打到了
                bool mh = false;
                if (bullets[bul_i].spd > 0) {
                    if (bullets[bul_i].mh)
                        mh = meihuo (zmb_i);
                    else {
                        int g=bullets[bul_i].attack;
                        if(zombies[zmb_i].type==19&&bullets[bul_i].color == QColor (0, 0, 0))g=5;//气球僵尸
                        zombies[zmb_i].life -= g;
                        if (bullets[bul_i].freeze)
                            zombies[zmb_i].freeze = 0;
                        else if (bullets[bul_i].color == QColor (255, 0, 0)||bullets[bul_i].color == QColor (254, 0, 0)) {
                            if (zombies[zmb_i].freeze < 100 && zombies[zmb_i].eat)
                                zombies[zmb_i].life -= 2; //冰火叠加增加1伤害
                            if(zombies[zmb_i].type==12)
                                zombies[zmb_i].life -= g*1.5;//冰车怕火
                            zombies[zmb_i].freeze = 100;//取消寒冰
                        }
                    }
                }
                else {//魅惑子弹
                    int z = bullets[bul_i].attack;
                    if (zombies[zmb_i].life < z)
                        z = zombies[zmb_i].life;
                    if (zombies[zmb_i].type == 11 && bullets[bul_i].siz == -2)//小车不杀死僵王
                        z = 0;
                    if(z&&bullets[bul_i].siz != -2)
                    {
                        z=(rand()%4==0?1:0);
                        bullets[bul_i].x += bullets[bul_i].spd * sd;
                        zombies[zmb_i].x=bullets[bul_i].x + 50;
                        zombies[zmb_i].eating=true;
                    }
                    bullets[bul_i].attack -= z;
                    zombies[zmb_i].life -= z;
                    //减去最小的
                }
                if (!mh) {
                    if (zombies[zmb_i].life <= 0) {
                        if (zombies[zmb_i].type == 15 && m_wnd->m_hd)//爆炸
                            Crepeer (zmb_i);
                        else
                            DestroyJs (zmb_i);
                        b += 1;
                    }
                    else
                    {
                        int od=zombies[zmb_i].id;
                        zombies[zmb_i].id = GetJsIcon (zombies[zmb_i], *this);
                        if(od!=zombies[zmb_i].id&&zombies[zmb_i].type == 17 && zombies[zmb_i].x <= 750) //张楠
                                ZhangNan(zmb_i);
                    }
                }
                if (bullets[bul_i].jian) //群体伤害
                {
                    int tx = zombies[zmb_i].x, ji = bullets[bul_i].jian;
                    int y = bullets[bul_i].y, z = (bullets[bul_i].freeze ? -1 : 1);
                    if (y > 0 && ji >= 2) {
                        if (ji == 3)
                            KillJs (tx - SI * 0.582, tx + SI * 1.582, y - 1, z);
                        else
                            KillJs (tx, y - 1, z);
                    }
                    if (y < MN - 1 && ji >= 2) {
                        if (ji == 3)
                            KillJs (tx - SI * 0.582, tx + SI * 1.582, y + 1, z);
                        else
                            KillJs (tx, y + 1, z);
                    }
                    if (ji == 3)
                        KillJs (tx - SI * 0.582, tx + SI * 1.582, y, z);
                    else
                        KillJs (tx, y, z);
                }
                if (bullets[bul_i].spd > 0 || bullets[bul_i].attack <= 0)//魅惑子弹消失
                    bullets[bul_i].y = -1;
                if (bullets[bul_i].spd < 0) {
                    Zombie zz;
                    zz.type = bullets[bul_i].siz;
                    zz.life = bullets[bul_i].attack;
                    zz.y = -1;zz.jump=0;
                    bullets[bul_i].zx = GetJsIcon (zz, *this);//更新子弹图像
                }
            }
            if (bullets[bul_i].x > MM * SI + 10) {
                if (bullets[bul_i].siz == -2)
                    car_driv[bullets[bul_i].y] = false;
                bullets[bul_i].y = -1;
            }
        }
    }
    int sa = 0, sb = 0;
    for (int i = 0; i < bul_cnt; i++) {
        if (bullets[i].y != -1)
            bul_tmp[sa++] = bullets[i];
    }
    for (int i = 0; i < zmb_cnt; i++) {
        if (zombies[i].life != -1)
            zmb_tmp[sb++] = zombies[i];
    }
    bul_cnt = sa;
    zmb_cnt = sb;
    for (int i = 0; i < sa; i++)
        bullets[i] = bul_tmp[i];
    for (int i = 0; i < ns; i++)
        bullets[bul_cnt++] = nw[i];
    for (int i = 0; i < sb; i++) {
        zombies[i] = zmb_tmp[i];
        zombies[i].num = i;
    }
    SZZ = false;
}

bool CGame::NotZw (int shu) //特殊植物
{
    int gj = -plants[shu].attack;
    bool rt = false;
    int n, cs[MAXSHU] = {0}, cf[MAXSHU], fs = 0, kx[8], ky[8], ks = 0, t,lx,rx;
    switch (gj) {
        case 1: //向日葵
            if (!m_wnd->imjs)
                m_yg += (m_wnd->sun_dou?50:25);
            break;
        case 2: //炸弹
            lazd[plants[shu].x / SI][plants[shu].y] = TM;
            for (int s = -1; s <= 1; s++)
                KillJs (plants[shu].x - 1.582 * SI, plants[shu].x + 1.582 * SI, plants[shu].y + s, 100);
            DestroyZw (shu);
            rt = true;
            break;
        case 3: //火爆辣椒
            lalj[plants[shu].y] = TM;
            KillJsLine (plants[shu].y, 90);
            DestroyZw (shu);
            rt = true;
            break;
        case 4: //窝瓜
        {
            bool rtn = false;
            {
                int t=GetFirstJs(plants[shu].x-SI*1.2,plants[shu].y);
                if(t!=-1&&zombies[t].x - plants[shu].x <= SI * 1.2)
                {
                    rtn = true;
                    if (zombies[t].x >=0&&zombies[t].x < MM*SI+100)
                        lawg.insert(make_pair(make_pair(zombies[t].x,plants[shu].y),TM));
                    KillJs (zombies[t].x - 1,zombies[t].x-1+SI*0.8,plants[shu].y, 80);//修复判定
                }
            }
            if (rtn)
                DestroyZw (shu), rt = true;
            break;
        }
        case 5: //阳光菇
            if (plants[shu].fu < 10)
                m_yg += (m_wnd->sun_dou?25:15);
            else
                m_yg += (m_wnd->sun_dou?50:25);
            plants[shu].fu += 1; //生长
            if (plants[shu].fu > 10)
                plants[shu].fu = 10;
            plants[shu].id = GetZwIcon (plants[shu]);
            break;
        case 6: //毁灭菇
            for (int s = -2; s <= 2; s++)
                KillJs (plants[shu].x - 2.582 * SI, plants[shu].x + 2.582 * SI, plants[shu].y + s, 100);
            DestroyZw (shu);
            rt = true;
            m_keng[plants[shu].x / SI][plants[shu].y] = 1; //坑
            break;
        case 7: //寒冰菇
            freeze_all = true;
            for (int i = 0; i < zmb_cnt; i++)
                zombies[i].freeze = 0;
            m_wnd->KillTimer (1006);
            m_wnd->SetTimer (1006, 6000, NULL);
            DestroyZw (shu);
            rt = true;
            break;
        case 8: //全息坚果
            plants[shu].life += (m_wnd->m_hd ? 6 : 10);
            if (plants[shu].life > 500 * (m_wnd->m_ea >= 2 ? 1.5 : 1))
                plants[shu].life = 500 * (m_wnd->m_ea >= 2 ? 1.5 : 1);
            plants[shu].id = GetZwIcon (plants[shu]);
            break;
        case 9: //双子
            m_yg += (m_wnd->sun_dou?50:25);
            break;
        case 10: //医疗
            for (int a = -1; a <= 1; a++) {
                for (int b = -1; b <= 1; b++) {
                    int z = GetZwShu (plants[shu].x + a * SI, plants[shu].y + b);
                    if (z != -1) {
                        if (plants[z].type == 25 || plants[z].type == 32)
                            continue;
                        bool bk = false;
                        if (!m_wnd->imjs)
                            m_yg -= (m_wnd->m_hd ? 3 : 2);
                        if (m_yg < 0) {
                            m_yg = 0;
                            goto ov;
                        }
                        if (plants[z].life <= 200) {
                            if (plants[z].life < 200)
                                bk = true;
                            plants[z].life += (m_wnd->m_ea > 1 ? 3+rand()%2 : 2+rand()%2);
                            if (plants[z].life > 200)
                                plants[z].life = 200;
                        }
                        else {
                            if (plants[z].life < 1800 * (m_wnd->m_ea >= 2 ? 2 : 1))
                                bk = true;
                            plants[z].life += (m_wnd->m_hd ? 15 : 18) + 2 * m_wnd->m_ea - 2;
                            if (plants[z].life > 1800 * (m_wnd->m_ea >= 2 ? 2 : 1))
                                plants[z].life = 1800 * (m_wnd->m_ea >= 2 ? 2 : 1);
                        }
                        int tx = (plants[shu].x + a * SI) / SI, ty = plants[shu].y + b;
                        if (m_ng[tx][ty]) {
                            if (m_ng[tx][ty] < 500)
                                bk = true;
                            m_ng[tx][ty] += 10;
                            if (m_ng[tx][ty] > 500)
                                m_ng[tx][ty] = 500;
                        }
                        if (!bk && !m_wnd->imjs)
                            m_yg += (m_wnd->m_hd ? 3 : 2);
                        plants[z].id = GetZwIcon (plants[z]);
                    }
                }
            }
        ov:
            break;
        case 11: //忧郁菇
            plants[shu].tag = 0;
            t = (plants[shu].fei >= 4 ? 2 : 1);
            for (int s = -t; s <= t; s++) {
                if (KillJs (plants[shu].x - (t + 0.582) * SI, plants[shu].x + (t + 0.582) * SI, plants[shu].y + s, 3))
                    plants[shu].tag = 1;
            }
            break;
        case 12: //双头喷
            plants[shu].tag = KillJsLine (plants[shu].y, plants[shu].fei >= 4 ? -1 : 1);
            if (plants[shu].fei >= 3)
                plants[shu].tag = (plants[shu].tag || KillJsLie (plants[shu].x, plants[shu].fei >= 4 ? -1 : 1));
            break;
        case 13: //大葱
            for (int a = -1; a <= 1; a++) {
                for (int b = -1; b <= 1; b++) {
                    if (a == 0 && b == 0)
                        continue;
                    int z = GetZwShu (plants[shu].x + a * SI, plants[shu].y + b);
                    if (z == -1) {
                        if (plants[shu].x + a * SI >= 0 && plants[shu].x + a * SI <= (MM - 1) * SI && plants[shu].y + b >= 0 && plants[shu].y + b < MN) {
                            kx[ks] = plants[shu].x + a * SI;
                            ky[ks++] = plants[shu].y + b;
                        }
                        continue;
                    }
                    cs[plants[z].type] += 1;
                }
            }
            if (ks) {
                for (int i = 0; i < MAXSHU; i++) {
                    if (cs[i] >= 2)
                        cf[fs++] = i;
                }
                if (fs) {
                    int z = cf[rand () % fs], t;
                    for (t = 0; t < MAXSHU; t++) {
                        if (m_wnd->plants[t].type == z)
                            break;
                    }
                    int nz = rand () % ks;
                    Plant zw = m_wnd->plants[t];
                    zw.x = kx[nz];
                    zw.y = ky[nz];
                    m_yg += zw.cost;
                    NewZw (zw);
                }
            }
            break;
        case 14: { //炮
            int t = GetZwShu (plants[shu].x - SI, plants[shu].y);
            if (t != -1 && plants[t].type == 31)//左边有前一半
                plants[shu].tag += 1;
            if (plants[shu].tag > 10)
                plants[shu].tag = 10;
        } break;
        case 16:
        if(plants[shu].fu)plants[shu].fu-=1;
        else
        {
            int w=-1,xz=SI*1.7,mi=1e9;
            for(int i=0;i<zmb_cnt;i++)
            {
                if(abs(zombies[i].x-plants[shu].x)<=xz&&abs(zombies[i].y-plants[shu].y)<=2&&zombies[i].life>10&&(zombies[i].type==2||zombies[i].type==3||zombies[i].type==7))
                {
                    int t=abs(zombies[i].x-plants[shu].x)+SI*abs(zombies[i].y-plants[shu].y);
                    if(t<mi)mi=t,w=i;
                }
            }
            if(w!=-1)
            {
                zombies[w].life=10;plants[shu].fu=24;
                zombies[w].id=GetJsIcon(zombies[w],*this);
            }
        }
        plants[shu].id = GetZwIcon (plants[shu]);
        break;
    case 17:
        lx=plants[shu].x - 1.582 * SI;rx=plants[shu].x + 1.582 * SI;
        for(int i=0;i<zmb_cnt;i++)
        {
            if(zombies[i].x>=lx&&zombies[i].x<=rx&&zombies[i].y>=plants[shu].y-1&&zombies[i].y<=plants[shu].y+1&&zombies[i].type!=11&&zombies[i].type<100)
            {
                meihuo(i);
                i-=1;
            }
        }
        DestroyZw (shu);
        rt = true;
        break;
    }
    return rt;
}
bool CGame::KillJs (int x, int y, int kill) {
    return KillJs (x - SI * 0.582, x + SI * 0.582, y, kill);
}
bool CGame::KillJs (int l, int r, int y, int kill) //伤害僵尸
{
    if (y < 0 || y >= MN)
        return false;
    bool rtn = false;
    for (int i = 0; i < zmb_cnt; i++) {
        if (zombies[i].life < 0)
            continue;
        if(kill!=100&&zombies[i].type==19)continue;
        if (zombies[i].y == y && zombies[i].x >= l && zombies[i].x <= r && zombies[i].type < 100) {
            rtn = true;
            if (kill > 0)
                zombies[i].life -= kill;
            else {//负数表示寒冰
                zombies[i].life += kill;
                zombies[i].freeze = 0;
            }
            if (zombies[i].life <= 0) {
                DestroyJs (i);
                if (!SZZ)
                    i -= 1;
            }
            else
                zombies[i].id = GetJsIcon (zombies[i], *this);
        }
    }
    return rtn;
}
bool CGame::KillJsLie (int x, int kill) { //列伤害
    bool rtn = false;
    int l = x - SI * 0.4, r = x + SI * 0.4;
    for (int i = 0; i < zmb_cnt; i++) {
        if (zombies[i].life < 0)
            continue;
        if(zombies[i].type==19)continue;
        if (zombies[i].x >= l && zombies[i].x <= r && zombies[i].type < 100) {
            rtn = true;
            if (kill > 0)
                zombies[i].life -= kill;
            else {//负数表示寒冰
                zombies[i].life += kill;
                zombies[i].freeze = 0;
            }
            if((kill==1||kill==-1)&&zombies[i].type==7&&!m_wnd->m_hd)//穿透
                zombies[i].life/=1.2;
            if (zombies[i].life <= 0) {
                DestroyJs (i);
                if (!SZZ)
                    i -= 1;
            }
            else
                zombies[i].id = GetJsIcon (zombies[i], *this);
        }
    }
    return rtn;
}
bool CGame::KillJsLine (int y, int kill) //行伤害
{
    bool rt = false;
    for (int i = 0; i < zmb_cnt; i++) {
        if (zombies[i].life == -1 || zombies[i].type == 11 || zombies[i].type >= 100)
            continue;
        if(zombies[i].type==19&&kill != 90)continue;
        if (zombies[i].y == y) {
            if (kill > 0)
                zombies[i].life -= kill;
            else { //负数表示寒冰
                zombies[i].life += kill;
                zombies[i].freeze = 0;
            }
            if((kill==1||kill==-1)&&zombies[i].type==7&&!m_wnd->m_hd)//穿透
                zombies[i].life/=1.2;
            rt = true;
            if (kill == 90)
                zombies[i].freeze = 100;
            if (zombies[i].life <= 0) {
                DestroyJs (i);
                i -= 1;
            }
            else
                zombies[i].id = GetJsIcon (zombies[i], *this);
        }
    }
    if (kill == 90) { //烧掉冰
        for (int x = 0; x < MM; x++)
            m_ice[x][y] = -1e8;
    }
    return rt;
}
void CGame::KillAll () {
    while (plt_cnt > 0)
        DestroyZw (0);
    while (zmb_cnt > 0)
        DestroyJs (0);
    while (bul_cnt > 0)
        DestroyZd (0);
}

void CGame::ClearCd()
{
    for (int i = 0; i < MM; i++) {
        for (int j = 0; j < MN; j++) {
            m_keng[i][j] = m_ng[i][j] = 0;
            m_dc[i][j] = 0;
            m_ice[i][j] = -1e8;
            m_wnd->keep[i][j]=0;
        }
    }
    for (int i = 0; i < MN; i++)
        iswa[i] = false;
}

void CGame::CreateGame (int lx) {
    ClearCd();
    if(m_wnd->b_guan)
    {
        if(SI==110)
            MN=6,MM=11,SI=89;
        else
            MN=12,MM=22,SI=45;
        ClearCd();
    }
    if (lx == 4) {//水池
        if(SI==110)
        {
            MN=6;MM=10;SI=91;
            ClearCd();
            for(int i=0;i<MM;i++)
            {
                for(int j=2;j<4;j++)
                    m_keng[i][j] = (m_wnd->m_ai ? 3 : 2), iswa[j] = true;
            }
        }
        else
        {
            MN=12;MM=22;SI=45;
            ClearCd();
            for(int i=0;i<MM;i++)
            {
                for(int j=4;j<8;j++)
                    m_keng[i][j] = (m_wnd->m_ai ? 3 : 2), iswa[j] = true;
            }
        }
    }
    else if (endless) {//无尽水池
        for (int i = 0; i < MM; i++) {
            m_keng[i][1] = (m_wnd->m_ai ? 3 : 2), iswa[1] = true;
            m_keng[i][MN - 2] = (m_wnd->m_ai ? 3 : 2), iswa[MN - 2] = true;
            if (SI == 55) {
                m_keng[i][2] = (m_wnd->m_ai ? 3 : 2), iswa[2] = true;
                m_keng[i][MN - 3] = (m_wnd->m_ai ? 3 : 2), iswa[MN - 3] = true;
            }
        }
    }
    wat_cnt = 0;
    for (int i = 0; i < MN; i++) {
        if (iswa[i])
            wat_row[wat_cnt++] = i;
    }
    for (int x = 0; x < MM * SI + 100; x++) {
        for (int y = 0; y < MN; y++)
            fir_plt[x][y] = plt_num[x][y] = -1;
    }
    for (int i = 0; i < MN; i++)
        row_zmb[i] = lst[i] = 0;
    zmb_cnt = plt_cnt = bul_cnt = t_ai = m_cc =XS=0;
    m_yg = 50;
    if(m_wnd->m_hd)m_yg+=50;
    if(m_wnd->m_tui)m_yg+=50;
    for (int i = 0; i < MN; i++) {
        car[i] = true;
        car_driv[i] = false;
    }
    for (int i = 0; i < MN; i++) {
        lalj[i] = -1e9;
        for (int j = 0; j < MM; j++)
            lazd[j][i] = lagz[j][i]=-1e9;
        lawg.clear();
    }
    started = true;
    freeze_all = SZZ = over = zk= false;
    scene = lx;
    if (m_wnd->is11) //生成BOSS
    {
        Zombie j;
        j.Create (0, (SI <= 55 ? 3000 : 1000), 10000, (MM - 1) * SI + 5, 0, 54, 11);
        for (int n = 0; n < MN; n++) {
            j.y = n;
            NewJs (j);
        }
    }
}

void CGame::input (QDataStream &ar) {
    ar >> plt_cnt;
    for (int i = 0; i < plt_cnt; i++)
        plants[i].input (ar);
    ar >> zmb_cnt;
    for (int i = 0; i < zmb_cnt; i++)
        zombies[i].input (ar);
    ar >> bul_cnt;
    for (int i = 0; i < bul_cnt; i++)
        bullets[i].input (ar);
    ar >> m_yg;
    ar >> scene;
    for (int i = 0; i < MM; i++) {
        for (int j = 0; j < MN; j++)
            ar >> m_keng[i][j] >> m_ng[i][j] >> m_dc[i][j] >> lazd[i][j]  >> m_ice[i][j]>>lagz[i][j];
    }
    for (int i = 0; i < MN; i++) {
        ar >> car[i] >> car_driv[i] >> iswa[i];
        ar >> lst[i] >> lalj[i];
    }
    ar >> freeze_all >> t_ai >> endless >> zk;
    wat_cnt = 0;
    for (int i = 0; i < MN; i++) {
        if (iswa[i])
            wat_row[wat_cnt++] = i;
    }
}
void CGame::output (QDataStream &ar) {
    ar << plt_cnt;
    for (int i = 0; i < plt_cnt; i++)
        plants[i].output (ar);
    ar << zmb_cnt;
    for (int i = 0; i < zmb_cnt; i++)
        zombies[i].output (ar);
    ar << bul_cnt;
    for (int i = 0; i < bul_cnt; i++)
        bullets[i].output (ar);
    ar << m_yg;
    ar << scene;
    for (int i = 0; i < MM; i++) {
        for (int j = 0; j < MN; j++)
            ar << m_keng[i][j] << m_ng[i][j] << m_dc[i][j] << lazd[i][j] << m_ice[i][j]<<lagz[i][j];
    }
    for (int i = 0; i < MN; i++) {
        ar << car[i] << car_driv[i] << iswa[i];
        ar << lst[i] << lalj[i];
    }
    ar << freeze_all << t_ai << endless << zk;
    lawg.clear();
}
